import mx.collections.XMLListCollection;
import mx.rpc.events.ResultEvent;

public var xmlcol_Board:XMLListCollection = new XMLListCollection();
private function getBoard_result(event:ResultEvent):void{
	bi_1.visible = false;
	xmlcol_Board.removeAll();
	xmlcol_Board = new XMLListCollection(new XMLList(event.result.board));
	
	list_board.dataProvider = xmlcol_Board;
}
private function setBoard_result(event:ResultEvent):void{
	bi_1.visible = false;
	tarea_board_context.text = "";
	onClickBack();
	loadBoard();
}
private function updateBoard_result(event:ResultEvent):void{
	bi_1.visible = false;
	onClickBack();
	loadBoard();
}
private function delBoard_result(event:ResultEvent):void{
	bi_1.visible = false;
	onClickBack();
	loadBoard();
}
public var xmlcol_BoardReply:XMLListCollection = new XMLListCollection();
private function getBoardReply_result(event:ResultEvent):void{
	bi_1.visible = false;
	xmlcol_BoardReply.removeAll();
	xmlcol_BoardReply = new XMLListCollection(new XMLList(event.result.board_reply));
	list_board_reply.dataProvider = xmlcol_BoardReply;
	
}
private function setBoardReply_result(event:ResultEvent):void{
	bi_1.visible = false;
	tarea_board_reply_context.text = "";
	onClickBack();
	loadBoardReply();
}
private function updateBoardReply_result(event:ResultEvent):void{
	bi_1.visible = false;
	onClickBack();
	loadBoardReply();
}
private function delBoardReply_result(event:ResultEvent):void{
	bi_1.visible = false;
	onClickBack();
	loadBoardReply();
}


private function loadBoard():void{
	bi_1.visible = true;
	
	var params:Object = new Object();
	params['category'] = current_board;
	httpserv_getBoard.send(params);
}
private function onClickBoardWrite():void{
	if(chkAccessPermission(escape("준회원"))){
		go_board_view('write');
	}else{
		if(!alertShowYb){
			Alert.show(this, "준회원 이상만 글작성이 가능합니다. 가입 후, 로그인해주십시오.");
			alertShowYb	= true;
		}
		
	}
}
private function onClickBoard():void{
	go_board_view('view');
}
private function onClickSendBoard():void{
	bi_1.visible = true;
	var params:Object = new Object();
	params['id'] = mId;
	params['category'] = current_board;
	params['context'] = escape(tarea_board_context.text);
	httpserv_setBoard.send(params);
}
private function onClickModifyBoard():void{
	bi_1.visible = true;
	var params:Object = new Object();
	params['no'] = xmlcol_Board[list_board.selectedIndex].child('no');
	params['context'] = escape(tarea_board_context.text);
	httpserv_updateBoard.send(params);
}
private function onClickDelBoard():void{
	bi_1.visible = true;
	var params:Object = new Object();
	params['no'] = xmlcol_Board[list_board.selectedIndex].child('no');
	httpserv_delBoard.send(params);
}
private function onClickBoardReplyWrite():void{
	if(chkAccessPermission(escape("준회원"))){
		go_board_reply_view('write');
	}else{
		if(!alertShowYb){
			Alert.show(this, "준회원 이상만 댓글 작성이 가능합니다. 가입 후, 로그인해주십시오.");
			alertShowYb	= true;
		}	
	}
}
private function onClickBoardReply():void{
	go_board_reply_view('view');
}
private function onClickSendBoardReply():void{
	bi_1.visible = true;
	var params:Object = new Object();
	params['id'] = mId;
	params['board_no'] = xmlcol_Board[list_board.selectedIndex].child('no');
	params['context'] = escape(tarea_board_reply_context.text);
	httpserv_setBoardReply.send(params);
	
}
private function onClickModifyBoardReply():void{
	bi_1.visible = true;
	var params:Object = new Object();
	params['no'] = xmlcol_BoardReply[list_board_reply.selectedIndex].child('no');
	params['context'] = escape(tarea_board_reply_context.text);
	httpserv_updateBoardReply.send(params);
}
private function onClickDelBoardReply():void{
	bi_1.visible = true;
	var params:Object = new Object();
	params['no'] = xmlcol_BoardReply[list_board_reply.selectedIndex].child('no');
	httpserv_delBoardReply.send(params);
}
private function loadBoardReply():void{
	bi_1.visible = true;
	var params:Object = new Object();
	params['board_no'] = xmlcol_Board[list_board.selectedIndex].child('no');
	httpserv_getBoardReply.send(params);
}
private function go_board_view(mode:String):void{
	this.currentState = "board_view";
	tarea_board_context.editable = false;
	img_board_send.visible = false; img_board_send.includeInLayout = false;
	img_board_modify.visible = false; img_board_modify.includeInLayout = false;
	img_board_delete.visible = false; img_board_delete.includeInLayout = false;
	img_board_reply_write.visible = false; img_board_reply_write.includeInLayout = false;
	list_board_reply.visible = false; list_board_reply.includeInLayout = false;
	switch(mode){
		case "write":
			
			tarea_board_context.text = "글 내용";
			tarea_board_context.editable = true;
			lab_board_id.text = "작성자 : "+mId;
			img_board_send.visible = true; img_board_send.includeInLayout = true;
			img_board_modify.visible = false; img_board_modify.includeInLayout = false;
			img_board_delete.visible = false; img_board_delete.includeInLayout = false;
			img_board_reply_write.visible = false; img_board_reply_write.includeInLayout = false;
			list_board_reply.visible = false; list_board_reply.includeInLayout = false;
			
			break;
		case "view":
			loadBoardReply();
			tarea_board_context.text = xmlcol_Board[list_board.selectedIndex].child('context');
			if(mId == xmlcol_Board[list_board.selectedIndex].child('id')){
				tarea_board_context.editable = true;
			}else{
				tarea_board_context.editable = false;
			}
			lab_board_id.text = "작성자 : "+xmlcol_Board[list_board.selectedIndex].child('id');
			img_board_send.visible = false; img_board_send.includeInLayout = false;
			if(mId == xmlcol_Board[list_board.selectedIndex].child('id')){
				img_board_modify.visible = true; img_board_modify.includeInLayout = true;
				img_board_delete.visible = true; img_board_delete.includeInLayout = true;
			}else{
				img_board_modify.visible = false; img_board_modify.includeInLayout = false;
				img_board_delete.visible = false; img_board_delete.includeInLayout = false;
			}
			img_board_reply_write.visible = true; img_board_reply_write.includeInLayout = true;
			list_board_reply.visible = true; list_board_reply.includeInLayout = true;
			break;
	}
}
private function go_board_reply_view(mode:String):void{
	this.currentState = "board_reply_view";
	tarea_board_reply_context.editable = false;
	img_board_reply_send.visible = false; img_board_reply_send.includeInLayout = false;
	img_board_reply_modify.visible = false; img_board_reply_modify.includeInLayout = false;
	img_board_reply_delete.visible = false; img_board_reply_delete.includeInLayout = false;
	switch(mode){
		case "write":
			tarea_board_reply_context.text = "댓글 내용";
			tarea_board_reply_context.editable = true;
			lab_board_reply_id.text = "작성자 : "+mId;
			img_board_reply_send.visible = true; img_board_reply_send.includeInLayout = true;
			img_board_reply_modify.visible = false; img_board_reply_modify.includeInLayout = false;
			img_board_reply_delete.visible = false; img_board_reply_delete.includeInLayout = false;
			break;
		case "view":
			tarea_board_reply_context.text = xmlcol_BoardReply[list_board_reply.selectedIndex].child('context');
			if(mId == xmlcol_BoardReply[list_board_reply.selectedIndex].child('id')){
				tarea_board_reply_context.editable = true;
			}else{
				tarea_board_reply_context.editable = false;
			}
			lab_board_reply_id.text = "작성자 : "+xmlcol_BoardReply[list_board_reply.selectedIndex].child('id');
			img_board_reply_send.visible = false; img_board_reply_send.includeInLayout = false;
			if(mId == xmlcol_BoardReply[list_board_reply.selectedIndex].child('id')){
				img_board_reply_modify.visible = true; img_board_reply_modify.includeInLayout = true;
				img_board_reply_delete.visible = true; img_board_reply_delete.includeInLayout = true;
			}else{
				img_board_reply_modify.visible = false; img_board_reply_modify.includeInLayout = false;
				img_board_reply_delete.visible = false; img_board_reply_delete.includeInLayout = false;
			}
			break;
	}
}

private function chkAccessPermission(boardPerm:String):Boolean{
	if(permStrToInt(unescape(mPermission)) >= permStrToInt(unescape(boardPerm))){
		
		return true;
	}else{
		return false;
	}
}

private function permStrToInt(str:String):int{
	switch(str){
		case "준회원":
			return 1;	break;
		case "정회원":
			return 2;	break;
		case "우등회원":
			return 3;	break;
		case "우수회원":
			return 4;	break;
		case "최우수회원":
			return 5;	break;
		case "특별회원":
			return 6;	break;
		case "운영자":
			return 7;	break;
		default:
			return 0;	break;
	}
	return 0;
}